package com.android.WuxiBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

//http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html
public class RouteListAdapter extends BaseAdapter {

	public final class ViewHolder{
		public TextView schema;
		public TextView distance;
		public TextView line_1;
		public TextView line_2;
		public TextView route_1;
		public TextView route_2;
	}

	private LayoutInflater m_Inflater;
	private List<Map<String, Object>> m_Data;

	public RouteListAdapter(Context context)
	{
		m_Inflater = LayoutInflater.from(context);
		m_Data = new ArrayList<Map<String, Object>>();
	}

	public RouteListAdapter(Context context, List<Map<String, Object>> data)
	{
		m_Inflater = LayoutInflater.from(context);
		m_Data = data;
	}

	public void add(Map<String, Object> map)
	{
		m_Data.add(map);
	}
	
	public void updateItem(int position, String key, Object obj)
	{
		if (position < m_Data.size())
		{  
            m_Data.get(position).put(key, obj);  
        } 
	}

	public int getCount() {
		return m_Data.size();
	}

	public Object getItem(int position) {
		if (position < m_Data.size())
		{  
            return m_Data.get(position);  
        } 
		else  
        {
            return null;  
        }
	}

	public long getItemId(int position) {
		if (position < m_Data.size())
		{  
            return position;  
        } 
		else  
        {
            return -1;  
        }
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) 
		{
			holder=new ViewHolder();  
			
			convertView = m_Inflater.inflate(R.layout.route_vlist, null);
			holder.schema = (TextView)convertView.findViewById(R.id.TextViewSchema);
			holder.distance = (TextView)convertView.findViewById(R.id.RouteCountTotal);
			holder.line_1 = (TextView)convertView.findViewById(R.id.RouteLine_1);
			holder.line_2 = (TextView)convertView.findViewById(R.id.RouteLine_2);
			holder.route_1 = (TextView)convertView.findViewById(R.id.RouteInof_1);
			holder.route_2 = (TextView)convertView.findViewById(R.id.RouteInof_2);

			convertView.setTag(holder);
		}
		else 
		{
			holder = (ViewHolder)convertView.getTag();
		}
		
		String strLine_2 = (String)m_Data.get(position).get("line_2");
		
		holder.schema.setText((String)m_Data.get(position).get("schema"));
		holder.distance.setText((SpannableString)m_Data.get(position).get("distance"));
		holder.line_1.setText((String)m_Data.get(position).get("line_1"));
		holder.line_2.setText((String)m_Data.get(position).get("line_2"));
		holder.route_1.setText((String)m_Data.get(position).get("route_1"));
		holder.route_2.setText((String)m_Data.get(position).get("route_2"));
		
		if(strLine_2 == null || strLine_2.toString().equals(""))
		{
			holder.line_2.setVisibility(View.GONE);
			holder.route_2.setVisibility(View.GONE);
		}
		else
		{
			holder.line_2.setVisibility(View.VISIBLE);
			holder.route_2.setVisibility(View.VISIBLE);
		}

		
		return convertView;
	}

}
